package mesweb.mesweb.controller.produce;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.dto.ProducePlan.DayReportDetailConditionDto;
import mesweb.mesweb.entity.produce.ProduceDayReportDetail;
import mesweb.mesweb.service.produce.IProduceDayReportDetailService;
import mesweb.mesweb.vo.ProducePlan.DayReportDetailResultVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 日报工
 * @Author: jeecg-boot
 * @Date:   2021-08-17
 * @Version: V1.0
 */

@Api(tags="日报工")
@RestController
@RequestMapping("/mes/ProduceDayReportDetail")
public class ProduceDayReportDetailController {
	@Autowired
	private IProduceDayReportDetailService ProduceDayReportDetailService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<ProduceDayReportDetail> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = ProduceDayReportDetailService.list();
        } else {
            QueryWrapper<ProduceDayReportDetail> queryWrapper = new QueryWrapper<>();
            data = ProduceDayReportDetailService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param ProduceDayReportDetail
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="日报工-分页列表查询", notes="日报工-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(ProduceDayReportDetail ProduceDayReportDetail,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<ProduceDayReportDetail> queryWrapper = new QueryWrapper<>();
		Page<ProduceDayReportDetail> pageProduceDayReportDetail = new Page<ProduceDayReportDetail>(page, size);
		IPage<ProduceDayReportDetail> pageList = ProduceDayReportDetailService.getProduceDayReportDetail(pageProduceDayReportDetail, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param ProduceDayReportDetail
	 * @return
	 */
	@ApiOperation(value="日报工-编辑", notes="日报工-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody ProduceDayReportDetail ProduceDayReportDetail) {
		boolean result = false;
       try {
		   if(ProduceDayReportDetail.getId() == 0L) {
			   QueryWrapper<ProduceDayReportDetail> queryWrapper = new QueryWrapper<>();
			   queryWrapper.eq("order_no", ProduceDayReportDetail.getOrderNo());
			   queryWrapper.eq("product_no", ProduceDayReportDetail.getProductNo());
			   queryWrapper.eq("procedure_no", ProduceDayReportDetail.getProcedureNo());
			   queryWrapper.eq("report_date", ProduceDayReportDetail.getReportDate());
			   ProduceDayReportDetail mesTechnologyFlow1 = ProduceDayReportDetailService.getOne(queryWrapper);
			   if (mesTechnologyFlow1 != null) {
				   String msg = StrUtil.format("订单{},产品{},工序{},最晚上报日期或者名称{}已经存在！", mesTechnologyFlow1.getOrderNo(),mesTechnologyFlow1.getProductNo(),mesTechnologyFlow1.getProcedureNo(),mesTechnologyFlow1.getReportDate());
				   throw new ApiException(msg);
			   }
			   result = ProduceDayReportDetailService.save(ProduceDayReportDetail);
		   }
		   else
		   {
			   result = ProduceDayReportDetailService.updateById(ProduceDayReportDetail);
		   }
		   return CommonResult.success("保存成功！");
	   }catch (Exception ex)
	   {
		   return CommonResult.failed(ex.getMessage());
	   }
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="日报工-通过id删除", notes="日报工-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		ProduceDayReportDetailService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="日报工-批量删除", notes="日报工-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("ProduceDayReportDetailIDs") @RequestBody List<Long> ids) {
		this.ProduceDayReportDetailService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="日报工-通过id查询", notes="日报工-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		ProduceDayReportDetail ProduceDayReportDetail = ProduceDayReportDetailService.getById(id);
		return CommonResult.success(ProduceDayReportDetail);
	}

	/**
	 * 获取某月某订单某产品的日报表
	 * @param  dayReportDetailConditionDto
	 * @return
	 */
	@ApiOperation(value = "订单产品排产-获取某月某订单某产品的日报表", notes = "订单产品排产-获取某月某订单某产品的日报表")
	@PostMapping(value = "/statDayReportDetail")
	public CommonResult<?> statDayReportDetail(@RequestBody DayReportDetailConditionDto dayReportDetailConditionDto) {
		List<DayReportDetailResultVo> result =  ProduceDayReportDetailService.statDayReportDetail(dayReportDetailConditionDto);
		return CommonResult.success(result);
	}

}
